import { type PaginationProps } from "@pureadmin/table";
import { computed, onMounted, reactive, ref } from "vue";
import {
  changeState,
  deletePermission,
  getPermissionList,
} from "@/api/permission";
import { Permission } from "@/api/model/permissionModel";
import { FormInstance } from "element-plus";
import { message } from "@/utils/message";

export function useOperator() {
  const formRef = ref<FormInstance>();
  const form: Permission = reactive({
    size: 10,
    current: 1,
  });
  const dataList = ref([]);
  const loading = ref(true);
  const s_loading = ref(false);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true,
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列"),
    },
    {
      label: "权限名称",
      prop: "name",
    },
    {
      label: "权限代码",
      prop: "code",
    },
    {
      label: "状态",
      prop: "state",
      width: 80,
      cellRenderer: ({ row, props }) => (
        <el-switch
          v-model={row.state}
          size={props.size}
          inline-prompt
          loading={s_loading.value}
          style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
          active-text="启用"
          inactive-text="禁用"
          onChange={() => deptStateChange(row)}
        />
      ),
    },
    {
      label: "权限说明",
      prop: "description",
      showOverflowTooltip: true,
    },
    {
      label: "操作",
      fixed: "right",
      width: 200,
      slot: "operation",
    },
  ];
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary",
    ];
  });

  const deptStateChange = row => {
    s_loading.value = true;
    changeState(row.id, row.state)
      .then(res => {
        if (res) {
          message("角色已启用", { type: "success" });
        } else {
          message("角色已禁用", { type: "error" });
        }
      })
      .catch(e => {
        message(e.message, { type: "error" });
      })
      .finally(() => {
        s_loading.value = false;
      });
  };

  function handleSizeChange(val: number) {
    pagination.pageSize = val;
    onSearch();
  }

  function handleCurrentChange(val: number) {
    pagination.currentPage = val;
    onSearch();
  }

  function handleDelete(row) {
    deletePermission(row.id)
      .catch(err => {
        message(err.message, { type: "error" });
      })
      .finally(() => {
        onSearch();
      });
  }

  async function onSearch() {
    loading.value = true;
    form.current = pagination.currentPage;
    form.size = pagination.pageSize;
    const { records, total } = await getPermissionList(form);
    dataList.value = records;
    pagination.total = total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    pagination.currentPage = 1;
    pagination.pageSize = 10;
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    formRef,
    loading,
    columns,
    dataList,
    pagination,
    buttonClass,
    onSearch,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
    handleDelete,
  };
}
